package com.zareal.ymcc.web.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.zareal.ymcc.domain.KillCourse;
import com.zareal.ymcc.query.KillCourseQuery;
import com.zareal.ymcc.result.JSONResult;
import com.zareal.ymcc.result.PageList;
import com.zareal.ymcc.service.IKillCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/killCourse")
public class KillCourseController {

    @Autowired
    public IKillCourseService killCourseService;

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody KillCourse killCourse) {
        if (killCourse.getId() != null) {
            killCourseService.updateById(killCourse);
        } else {
            killCourseService.insert(killCourse);
        }
        return JSONResult.success();
    }

    /**
     * 添加秒杀课程
     */
    @RequestMapping(value = "/seckill", method = RequestMethod.POST)
    public JSONResult seckill(@RequestBody @Valid KillCourse killCourse) {
        killCourseService.seckill(killCourse);

        return JSONResult.success();
    }
    ///kill/killCourse/online/all
    /**
     * 添加秒杀课程
     */
    @RequestMapping(value = "/online/all", method = RequestMethod.GET)
    public JSONResult online() {
        List<KillCourse> killCourses =  killCourseService.online();

        return JSONResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        killCourseService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(killCourseService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(killCourseService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody KillCourseQuery query) {
        Page<KillCourse> page = new Page<KillCourse>(query.getPage(), query.getRows());
        page = killCourseService.selectPage(page);
        return JSONResult.success(new PageList<KillCourse>(page.getTotal(), page.getRecords()));
    }
}
